<?php

/**
 * Return folders.
 * 
 * @author Volker Kueffel <volker.kueffel@gmail.com>
 * @version $Id:get_folders.php 18 2009-01-05 02:18:30Z volker.kueffel $
 */
require_once('config.php');
require_once('LDAPConnector.class.php');

$ldap=LDAPConnector::init($config);

$result=array();
if ($ldap) {
	$dn='';
	if (array_key_exists('subdn',$_REQUEST) && $_REQUEST['subdn']) {
		$dn=$ldap->getFullDN($_REQUEST['subdn']);
	} else {
		$dn=$ldap->getBaseDN();
	}

	$results=$ldap->query($dn,'(objectClass=organizationalUnit)');
	if (is_array($results)) {
		$folders=array();
		$levels=count(split(',',$dn))+1;
		// find all on the requested level
		foreach ($results as $folder) {
			if (count(split(',',$folder['dn']))==$levels) {
				$node=new StdClass();
				$node->id=md5($folder['dn']);
				$node->text=$folder['ou'];	
				$node->leaf=false;
				$node->subdn=$ldap->extractSubDN($folder['dn']);
				$result[]=$node;
			}
		}
	}
}
echo json_encode($result);
?>